﻿using BLL;
using DevExpress.Xpo.DB.Helpers;
using Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 纺织贸易管理系统.其他窗体;
using 纺织贸易管理系统.新增窗体;
using 纺织贸易管理系统.设置窗体;

namespace 纺织贸易管理系统.报表窗体
{
    public partial class 盘亏出库查询 : Form
    {
        public 盘亏出库查询()
        {
            InitializeComponent();
            uiDatePicker2.Value = DateTime.Now;
            uiDatePicker1.Value = uiDatePicker1.Value.AddDays(-QueryTime.间隔);
            CreateGrid.Create(this.Name, gridView1);
            if (SysInfo.GetInfo.own != "审核制")
            {
                单据审核ToolStripMenuItem.Enabled = false;
            }
        }

        private void 配置列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new 配置列(gridView1) { formname = this.Name, Obj = new DanjuTable() };
            fm.ShowDialog();
        }

        private void 采购查询_Load(object sender, EventArgs e)
        {
            Query();
        }
        private void Query()
        {
            this.ShowWaitForm("正在查询，请等待.............");
            uiDatePicker2.Value = uiDatePicker2.Value.Date.AddDays(1).AddSeconds(-1);
            var dt = Connect.dbcontext.Ado.GetDataTable($"select * from danjutable,danjumingxitable where danjutable.rq between '{ uiDatePicker1.Value.Date }' and '{uiDatePicker2.Value }' and danjutable.ksmc like '%{txtkehu.Text}%' " +
                    $"and danjumingxitable.pingming like '%{txtpingming.Text.Replace("'", "''") }%' " +
                    $"and danjumingxitable.ganghao like '%{txtganghao.Text.Replace("'", "''") }%' " +
                    $"and danjumingxitable.yanse like '%{txtyanse.Text.Replace("'", "''") }%' " +
                      //$"and danjutable.djlx='{DanjuLeiXing.入库单  }' " +
                      $"and danjumingxitable.danhao like '%{FirstLetter.盘亏出库单}%' " +
                    $"and danjutable.dh=danjumingxitable.danhao  order by danjutable.id desc");
            gridControl1.DataSource = dt;
            this.HideWaitForm();
        }
        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(AccessBLL.CheckAccess (this.Name)==true)
            {
                MainForm.mainform.AddMidForm (new 盘亏出库单 () { useful=FormUseful.新增 });
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("修改盘亏出库单") == true)
            {
                if (单据BLL.检查是否已经审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) == false)
                {
                    MainForm.mainform.AddMidForm(new 盘亏出库单() { useful = FormUseful.修改, danju = DanjuTableService.GetOneDanjuTable(x => x.dh == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) });
                }
                else
                {
                    if (SysInfo.GetInfo.own != "审核制")
                    {
                        MainForm.mainform.AddMidForm(new 盘亏出库单() { useful = FormUseful.修改, danju = DanjuTableService.GetOneDanjuTable(x => x.dh == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) });
                    }
                    else
                    {
                        MessageBox.Show("对不起!改单据已经审核，如需修改请先取消审核状态！", this.Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Query();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("删除盘亏出库单"))
            {
                if ((int)(MessageBox.Show("您确定要删除改盘亏出库单吗？确定按YES.取消按NO", "提示", MessageBoxButtons.YesNo)) == 6)
                {
                    if (单据BLL.检查是否已经审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) == false)
                    {
                        try
                        {
                            Connect.dbcontext.BeginTran();
                            盘亏出库单BLL.删除单据(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString());
                            Connect.dbcontext.CommitTran();
                            Query();
                        }
                        catch (Exception ex)
                        {
                            Connect.dbcontext.RollbackTran();
                            Tools.ShowMessageBox.ShowErrorBox("审核的时候发送了错误！" + ex.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("该单据已经审核过，不能删除！要删除需先取消审核", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            else
            {
                ShowMessageBox.ShowErrorBox (  "您没有权限删除该单据！请让管理员为你开通");
            }
        }

        private void 导出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportFile.导出到文件(gridControl1,"采购单清单");
        }

        private void 审核通过ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //盘/*盈入库单BLL .单据审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString());*/
            if (AccessBLL.CheckAccess("审核盘亏出库单"))
            {
                if (单据BLL.检查是否已经审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) == false)
                {
                    try
                    {
                        Connect.dbcontext.BeginTran();
                        盘亏出库单BLL.单据审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString());
                        Connect.dbcontext.CommitTran();
                        MessageBox.Show("审核成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch(Exception ex)
                    {
                        Connect.dbcontext.RollbackTran();
                        Tools.ShowMessageBox.ShowErrorBox("审核的时候发送了错误！" + ex.Message);
                    }
                    Query();
                }
                else
                {
                    MessageBox.Show("该单据已经审核过了！无需再次审核", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("您没有权限审核改单据！请让管理员为你开通", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void 单据反审ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("反审核盘亏出库单"))
            {
                if (单据BLL.检查是否已经审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()))
                {
                    try
                    {
                        Connect.dbcontext.BeginTran();
                        盘亏出库单BLL.单据反审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString());
                        MessageBox.Show("反审核成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Query();
                    }
                    catch (Exception ex)
                    {
                        Connect.dbcontext.RollbackTran();
                        Tools.ShowMessageBox.ShowErrorBox("反审核的时候发送了错误！" + ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("该单据还未审核通过！不能反审核", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            MainForm.mainform.AddMidForm(new 盘亏出库单() { useful = FormUseful.查看 , danju = DanjuTableService.GetOneDanjuTable(x => x.dh == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) });
        }

        private void 保存样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name, gridView1);
        }

        private void 保存样式ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name, gridView1);
        }
    }
}
